import { t } from 'i18next'
import createVanilla from 'zustand/vanilla'
import create from 'zustand'
import { ChasingListModel, ListTranslator, _ListTranslator } from './translator'
import Alert from '@/components/Alert'
import { fetchGetChasingList, fetchPostChasingList } from '@/api/manager/chasing'
import { differenceBy } from 'lodash-es'
import { useMccStore } from '../../store'
import { Mt, MtEntity } from '@/api/manager/mt'

type ChasingStore = {
	list: ChasingListModel
	update: (d: Partial<ChasingStore>) => void
	fetchChasingList: () => void
	addList: (_list: MtEntity[]) => void
	updateList: (_list: MtEntity[]) => void
	postChasingList: () => void
	removeMt: (mtKeys: string[]) => void
	toggleClick: (mtKey: string) => void
	toggleCtrl: (mtKey: string) => void
	toggleShift: (mtKey: string) => void
	_start: string
	_changes: string[]
}
export const store = createVanilla<ChasingStore>((set, get) => ({
	list: [],
	_start: '',
	_changes: [],
	fetchChasingList: async () => {
		const meetingMoid = useMccStore.getState().mccInfo.meetingMoid
		let data = await fetchGetChasingList(meetingMoid)
		if (data) {
			let _list = data.map((x) => ({ ...x }))
			set({ list: _ListTranslator(_list) })
		} else {
			Alert(t('获取追呼列表失败'))
		}
	},
	postChasingList: async () => {
		const list = get().list
		let ary = list.map((item) => item.mcc.mtKey)
		const meetingMoid = useMccStore.getState().mccInfo.meetingMoid
		try {
			await fetchPostChasingList(meetingMoid, ary)
			Alert(t('设置追呼列表成功'))
		} catch (e) {
			Alert(t('设置追呼列表失败'))
		}
	},
	updateList: (updateList) => {
		let _updates = [...updateList]
		const newList = get().list
		for (const mt of _updates) {
			for (const item of newList) {
				if (item.mtKey === mt.mt_key) {
					item.mcc = new Mt(mt)
				}
			}
		}
		set({ list: [...newList] })
	},
	addList: (addList) => {
		let _updates = [...addList]
		const list = get().list
		let mts = ListTranslator(_updates)
		const insertList = differenceBy(mts, list, (x) => x.mtKey)
		set({ list: [...list.slice(0, list.length), ...insertList, ...list.slice(list.length)] })
	},
	update: (d) => set({ ...d }),
	/**移除终端*/
	removeMt: (mtKeys) => {
		const list = get().list
		const unRemove = list.filter((l) => !mtKeys.includes(l.mtKey))
		set({ list: unRemove })
	},
	toggleClick: (mtKey) => {
		const list = get().list
		list.forEach((item) => {
			if (item.mtKey === mtKey) {
				item.status.checked = true
			} else {
				item.status.checked = false
			}
		})
		set({ list: [...list], _start: mtKey, _changes: [] })
	},
	toggleCtrl: (mtKey) => {
		const list = get().list
		list.forEach((item) => {
			if (item.mtKey === mtKey) {
				item.status.checked = !item.status.checked
			}
		})
		set({ list: [...list], _start: mtKey, _changes: [] })
	},
	toggleShift: (mtKey) => {
		const { list, _changes, _start } = get()
		if (_changes.length > 0) {
			for (const item of list) {
				if (_changes.includes(item.mtKey)) {
					item.status.checked = false
				}
			}
		}
		let newChanges = []
		let startItem = list.find((x) => x.mtKey === _start)
		if (startItem) {
			if (startItem?.mtKey === mtKey) {
				startItem.status.checked = true
				newChanges.push(startItem?.mtKey)
			} else {
				let _flag = false,
					_time = 0
				for (const item of list) {
					if (item.mtKey === mtKey || item.mtKey === _start) {
						item.status.checked = true
						newChanges.push(item.mtKey)
						_flag = ++_time === 1
					}
					if (_flag) {
						item.status.checked = true
						newChanges.push(item.mtKey)
					}
				}
			}
		}
		set({ list: [...list], _changes: newChanges })
	},
}))
export const useChasingStore = create(store)
